package com.hph.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ResultUtils;
import com.hph.health.mapper.OrderMapper;
import com.hph.health.mapper.ProductMapper;
import com.hph.health.mapper.UserMapper;
import com.hph.health.model.entity.Order;
import com.hph.health.model.entity.Product;
import com.hph.health.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据大屏展示
 *
 * @author hanph
 * @from haut
 */
@RestController
@RequestMapping("/screen")
@Slf4j
public class ScreenController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private OrderMapper orderMapper;
    /**
     *
     * 获取用户的男女比例
     *
     * @return
     */
    @PostMapping("/users/gender")
    public BaseResponse<List<Map<String, Object>>> UserNumberList() {
        QueryWrapper<User> wrapper0 = new QueryWrapper<>();
        wrapper0.eq("gender", 0);
        int count0 = Math.toIntExact(userMapper.selectCount(wrapper0));
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("gender", 1);
        int count1 = Math.toIntExact(userMapper.selectCount(wrapper1));
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> map0 = new HashMap<>();
        map0.put("value", count0);
        map0.put("name", "男");
        resultList.add(map0);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("value", count1);
        map1.put("name", "女");
        resultList.add(map1);
        return ResultUtils.success(resultList);
    }

    /**
     * 按商品的类型比例
     *
     * @return
     */
    @PostMapping("/product/category")
    public BaseResponse<Map<String, Object>> ProductNumberList() {
        Map<String, Object> categoryCountMap = new HashMap<>();
        // 查询字段为category的数据条数
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("category", "count(1) as count");
        queryWrapper.groupBy("category");
        List<Map<String, Object>> categoryCountList = productMapper.selectMaps(queryWrapper);

        for (Map<String, Object> categoryCount : categoryCountList) {
            String category = (String) categoryCount.get("category");
            Long count = (Long) categoryCount.get("count");
            categoryCountMap.put(category, count);
        }

        return ResultUtils.success(categoryCountMap);
    }
    /**
     * 按商品的团购比例
     *
     * @return
     */
    @PostMapping("/product/isgroup")
    public BaseResponse<List<Map<String, Object>>> IsGroupNumberList() {
        QueryWrapper<Product> wrapper0 = new QueryWrapper<>();
        wrapper0.eq("is_group", 0);
        int count0 = Math.toIntExact(productMapper.selectCount(wrapper0));
        QueryWrapper<Product> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("is_group", 1);
        int count1 = Math.toIntExact(productMapper.selectCount(wrapper1));
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> map0 = new HashMap<>();
        map0.put("value", count0);
        map0.put("name", "正常");
        resultList.add(map0);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("value", count1);
        map1.put("name", "团购");
        resultList.add(map1);
        return ResultUtils.success(resultList);
}


    @PostMapping("/order/totalprice")
    public BaseResponse<BigDecimal> ordertotalprice() {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("total_price").eq("order_status", 3);
        List<Order> orders = orderMapper.selectList(queryWrapper);

        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Order order : orders) {
            totalPrice = totalPrice.add(order.getTotalPrice());
        }
        return ResultUtils.success(totalPrice);
    }

    @PostMapping("/order/number")
    public BaseResponse<Long> orderNumber() {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        Long count = orderMapper.selectCount(wrapper);
        return ResultUtils.success(count);
    }

    @PostMapping("/user/number")
    public BaseResponse<Long> userNumber() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_type", "user");
        Long count = userMapper.selectCount(wrapper);
        return ResultUtils.success(count);
    }
    @PostMapping("/merchant/number")
    public BaseResponse<Long> merchantNumber() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_type", "merchant");
        Long count = userMapper.selectCount(wrapper);
        return ResultUtils.success(count);
    }

}
